﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GetBestSeats
{
    public partial class MovieHall : Form
    {
        List<List<Seat>> hallrows = new List<List<Seat>>();

        public MovieHall()
        {
            InitializeComponent();

            // Sætter værdier i comboBox
            int comboVals = 0;
            while (comboVals < 20)
            {
                comboBox1.Items.Add(comboVals);
                comboVals++;
            }

            // Generate seats
            int rows = 0;
            int seatNumber = 0;
            while (rows < 10) {
                List<Seat> hallseats = new List<Seat>();

                while (seatNumber < 20) {
                    Seat s = new Seat(rows, seatNumber, true, 80.0);

                    // Optager sæder for testens skyld
                    if (rows == 5 && (seatNumber == 6 || seatNumber == 7)/** || rows == 5 && (seatNumber == 9 || seatNumber == 10)
                    || rows == 6 && (seatNumber == 9 || seatNumber == 10) || rows == 7 && (seatNumber == 9 || seatNumber == 10)
                        || rows == 3 && (seatNumber == 9 || seatNumber == 10)*/)
                    {
                        s.Available = false;
                    } 

                    hallseats.Add(s);
                    seatNumber++;
                }

                hallrows.Add(hallseats);
                seatNumber = 0;
                rows++;
            }


            // Find longest row
            int longestRow = 0;
            foreach (List<Seat> hr in hallrows) {
                if (longestRow < hr.Count()) 
                    longestRow = hr.Count();
            }

            // Adding columns
            int i = 0;
            while (i < longestRow) {
                dataGridView1.Columns.Add(i.ToString(), (i+1).ToString());
                i++;
            }

            // adding rows
            foreach (List<Seat> hr in hallrows)
            {
                DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
                foreach (Seat s in hr)
                {
                    row.HeaderCell.Value = (s.Row + 1).ToString();
                    if (s.Available)
                    {
                        row.Cells[s.Number].Style.BackColor = Color.Wheat;
                    }
                    else {
                        row.Cells[s.Number].Style.BackColor = Color.DarkRed;
                    }
                }
                dataGridView1.Rows.Add(row);
            }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<Seat> bestSeats = GetBestSeats(hallrows, Convert.ToInt32(comboBox1.SelectedItem.ToString()), 0);
            
            // Mark best seats
            foreach (Seat s in bestSeats) {
                dataGridView1.Rows[s.Row].Cells[s.Number].Style.BackColor = Color.Gold;
            }
        }

        private List<Seat> GetBestSeats(List<List<Seat>> movieHallSeats, int neededSeats, int changeRow) {

            // Seat list to return
            List<Seat> bestSeats = new List<Seat>();

            // Find center of hall
            int rowMiddle = (movieHallSeats.Count() / 2) + changeRow;
            int seatMiddle = movieHallSeats[rowMiddle].Count() / 2;

            // Check available
            if (movieHallSeats[rowMiddle][seatMiddle].Available)
            {
                // Add current seat to list
                bestSeats.Add(movieHallSeats[rowMiddle][seatMiddle]);
                int seatCount = 1;

                // Check if neighbor seats are free
                int i = 1;
                bool left = true;
                bool right = true;
                while (seatCount < neededSeats)
                {

                    // If needed number of seats weren't find, try again on new row
                    if (!left && !right)
                    {
                        if (changeRow > 0)
                            return GetBestSeats(movieHallSeats, neededSeats, changeRow * -1);
                        else
                            return GetBestSeats(movieHallSeats, neededSeats, (changeRow * -1) + 1);
                    }

                    // Check left neighbors
                    if (movieHallSeats[rowMiddle][seatMiddle - i].Available && left)
                    {
                        bestSeats.Add(movieHallSeats[rowMiddle][seatMiddle - i]);
                        seatCount++;
                    }
                    else left = false;

                    // Check right nightbors
                    if (movieHallSeats[rowMiddle][seatMiddle + i].Available && right && seatCount < neededSeats)
                    {
                        bestSeats.Add(movieHallSeats[rowMiddle][seatMiddle + i]);
                        seatCount++;
                    }
                    else right = false;

                    i++;
                }
                

            }
            else {
                if (changeRow > 0)
                    return GetBestSeats(movieHallSeats, neededSeats, changeRow * -1);
                else
                    return GetBestSeats(movieHallSeats, neededSeats, (changeRow * -1) + 1);
            }

            return bestSeats;
        }
    }
}
